﻿using Cl.Utils;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Cl.Utils.Primitives.Const.UserLogin;
using Cl.Utils.Helper;

namespace Cl.WebApi.Controllers
{
    public class ClientAdminController : BaseController
    {
        /// <summary>
        /// 执行时操作
        /// </summary>
        /// <param name="context"></param>
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            base.OnActionExecuting(context);

            // 如果基类已经设置了结果，则直接返回
            if (context.Result != null)
            {
                return;
            }

            if (GlobalService.SystemConfigGlobal.EnableSafeMode)
            {
                // 忽略权限验证
                if (JwtHelper.HasAllowAnonymous(context))
                {
                    base.OnActionExecuting(context);
                    return;
                }

                //校验用户是否是从客户端登陆
                if (GlobalUserManager.GlobalUserInfo.ClientType != ClientTypeConst.Admin)
                {
                    context.Result = new ContentResult() { StatusCode = StatusCodes.Status403Forbidden, Content = "权限不足" };
                    return;
                }
            }
        }
    }
}
